<?
include("../functions/functions.php");

if (isset($_POST['Year'])) {
	$thisYear = $_POST['Year']	;
}
else {
	$thisYear = date("Y");
}

$lastYear = $thisYear-1;
$reportTitle = "Total clients breached by Centrelink in the year of $lastYear/$thisYear.";

$totalStaff = 0;
$year = 0;
$site = 0;
$month = 0;
$year = 0;
$totalUniqueClients = 0;
$totalMonthly1 = 0;
$totalMonthly2 = 0;
$totalMonthly3 = 0;
$total1 = 0;
$total2 = 0;
$total3 = 0;
$site1 = "";
$site2 = "";
$site3 = "";
$lastMonth = "";
$monthlyTotal = 0;
$i = 0;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><? echo $reportTitle; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="reports.css" rel="stylesheet" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#0075C5" vlink="#0075C5" alink="#C5DDF4" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?
echo "<p style=\"text-align:center; font-size: 16pt\">$reportTitle</h3>";
?>
<table width="100%" border="0">
	<tr>
		<td width="50%">
			<form method="post">
				Select the financial year to report on: <select name="Year" onChange="submit();">
					<?
					$years = getBeginEndYears();
					for ($j = $years[start]; $j<$years[end]; $j++) {
						$option = ($j-1)." - ".$j;
						if ($thisYear == $j)
							$select = "SELECTED";
						else
							$select = "";
						echo "<option value=\"$j\" $select>$option</option>";
					}
					?>
				</select>
			</form>
		</td>
		<td align="right" width="50%"><? echo getCurrentDate(); ?></td>
	</tr>
</table>
<?

echo "<p>Total number of people assisted in a financial year for ER (clients and dependants)</p>";

echo "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"0\" border=\"1\" bordercolor=\"#000000\" class=\"body\" FRAME=BOX RULES=GROUPS>";

echo "<tbody><tr>
				<td class=\"bodybold\" style=\"width:50%;\">Site Name</td>
				<td class=\"bodybold\" style=\"width:50%;\">Site Total</td>
			</tr></tbody>";


$sql = "SELECT er_visit.client_id,er_visit.visit_date as firstVisit, month(er_visit.visit_date) as month, year(er_visit.visit_date) as year, er_visit.site_visited_id, er_site.site_name, no_adults_assistted, no_children_assistted
				FROM er_visit RIGHT OUTER JOIN er_site ON er_visit.site_visited_id = er_site.site_id
				WHERE income_status_id = '2' AND er_visit.visit_date >= '$lastYear-07-01' AND er_visit.visit_date <= '$thisYear-06-30'
				ORDER BY year, month, site_name, firstVisit";
//echo $sql;

$results = r_db($sql);
while ($row = mysql_fetch_array($results)) {
	$tempClientID = $row["client_id"];
	$tempFirstVisit = $row["firstvisit"];
	$tempSiteID = $row["site_visited_id"];
	$tempSiteName = $row["site_name"];
	$tempMonth = $row["month"];
	$tempYear = $row["year"];
	$tempAdultsAssisted = $row["no_adults_assistted"];
	$tempChildrenAssisted = $row["no_children_assistted"];
	$tempPeopleAssisted = $tempAdultsAssisted + $tempChildrenAssisted;

	$currentMonth = date("F",strtotime($tempFirstVisit));

	if ($site != $tempSiteID || $month != $tempMonth || $year != $tempYear) {

		if ($totalMonthly1 > 0) {
			echo "<td class=\"report_subheading\">";
			$monthlyTotal += $totalMonthly1;
			$total1 += $totalMonthly1;
			echo $totalMonthly1;
			$totalMonthly1 = 0;
			echo "</td>";
			echo "</tr>";
		}
		elseif ($totalMonthly2 > 0) {
			echo "<td class=\"report_subheading\">";
			$monthlyTotal += $totalMonthly2;
			$total2 += $totalMonthly2;
			echo $totalMonthly2;
			$totalMonthly2 = 0;
			echo "</td>";
			echo "</tr>";
		}
		if ($totalMonthly3 > 0) {
			echo "<td class=\"report_subheading\">";
			$monthlyTotal += $totalMonthly3;
			$total3 += $totalMonthly3;
			echo $totalMonthly3;
			$totalMonthly3 = 0;
			echo "</td>";
			echo "</tr>";
		}

		if ($tempMonth != $lastMonth) {
			if ($monthlyTotal > 0) {
				echo "<tr><td class=\"bodybold\">Total for this month</td><td class=\"bodybold\">$monthlyTotal</td></tr></tbody>";
				$monthlyTotal = 0;
			}
			$lastMonth = $tempMonth;
		}

		echo "<tr>";
		echo "<td class=\"report_subheading\">$tempSiteName, $currentMonth, $tempYear</td>";
		$site = $tempSiteID;
		$month = $tempMonth;
		$year = $tempYear;

		$i++;
	}

	if ($tempSiteID == 1) {
		$totalMonthly1+=$tempPeopleAssisted;
		$site1 = $tempSiteName;
	}
	elseif ($tempSiteID == 2) {
		$totalMonthly2+=$tempPeopleAssisted;
		$site2 = $tempSiteName;
	}
	elseif ($tempSiteID == 3) {
		$totalMonthly3+=$tempPeopleAssisted;
		$site3 = $tempSiteName;
	}
}

if ($totalMonthly1 > 0) {
	echo "<td class=\"report_subheading\">";
	$total1 += $totalMonthly1;
	echo $totalMonthly1;
	echo "</td>";
	echo "</tr>";
}
elseif ($totalMonthly2 > 0) {
	echo "<td class=\"report_subheading\">";
	$total2 += $totalMonthly2;
	echo $totalMonthly2;
	echo "</td>";
	echo "</tr>";
}
if ($totalMonthly3 > 0) {
	echo "<td class=\"report_subheading\">";
	$total3 += $totalMonthly3;
	echo $totalMonthly3;
	echo "</td>";
	echo "</tr>";
}

if ($totalMonthly1 > 0) {
	$monthlyTotal += $totalMonthly1;
	$totalMonthly1 = 0;
}
elseif ($totalMonthly2 > 0) {
	$monthlyTotal += $totalMonthly2;
	$totalMonthly2 = 0;
}
if ($totalMonthly3 > 0) {
	$monthlyTotal += $totalMonthly3;
	$totalMonthly3 = 0;
}

if ($monthlyTotal > 0) {
	echo "<tr><td class=\"bodybold\">Total for this month</td><td class=\"bodybold\">$monthlyTotal</td></tr></tbody>";
}
//$lastMonth = $tempMonth;

echo "<tbody>";
echo "<tr>";
echo "<td  class=\"bodybold\">";
echo "$site1";
echo "</td>";
echo "<td  class=\"bodybold\">";
echo "$total1";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td  class=\"bodybold\">";
echo "$site2";
echo "</td>";
echo "<td  class=\"bodybold\">";
echo "$total2";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td  class=\"bodybold\">";
echo "$site3";
echo "</td>";
echo "<td  class=\"bodybold\">";
echo "$total3";
echo "</td>";
echo "</tr>";
echo "</tbody>";

echo "<tbody>";
echo "<tr>";
echo "<td class=\"bodybold\">Total Clients Assisted:";
echo "</td>";
echo "<td class=\"bodybold\">";
echo $total1+$total2+$total3;
echo "</td>";
echo "</tr>";
echo "</tbody>";
?>
</body>
</html>
